package com.snailct.bpp.commons.utils;

import java.util.Date;
import java.util.List;
import java.util.Map;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.JSONUtils;


public class JsonUtils {
	

	/**
	 * 把对象转换成json格式字符串
	 * @param bean
	 * @return
	 */
    public static String beanToJson(Object bean){
        JsonConfig config = new JsonConfig();
        config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
        JSONObject jsonObject = JSONObject.fromObject(bean);
        return jsonObject.toString();
    }

    /**
     * json转成bean
     * @param jsonString
     * @param clazz
     * @param map
     * @return
     */
    public static Object jsonToBean(String jsonString,Class clazz,Map<String,Class> map){
        JSONObject jsonObject = JSONObject.fromObject(jsonString);
        JsonConfig config = new JsonConfig();
        config.setClassMap(map);
        config.setRootClass(clazz);
        //config.setJavaPropertyFilter(new InvisibleFilter("LIST"));
        Object bean = JSONObject.toBean(jsonObject,config);
        return bean;
    }

    public static String beanToJsonArray(Object beans){
        JsonConfig config = new JsonConfig();


        config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
        JSONArray jsonArray = JSONArray.fromObject(beans,config);
        return jsonArray.toString();
    }
    public static Object[] getDTOArray(String jsonString, Class clazz){
        JSONArray array = JSONArray.fromObject(jsonString);
        Object[] obj = new Object[array.size()];
        for(int i = 0; i < array.size(); i++){
            JSONObject jsonObject = array.getJSONObject(i);
            obj[i] = JSONObject.toBean(jsonObject, clazz);
        }
        return obj;
    }

    public static Map<String,Object> getDTOMap(String jsonStr){
        JSONObject  jasonObject = JSONObject.fromObject(jsonStr);
        Map<String,Object> map = (Map<String,Object>)jasonObject;
        return map;
    }



}
